package com.example.ross.security;

import com.example.ross.entity.SysUser;
import com.example.ross.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0.0
 * @className: UserDetailsServiceImpl
 * @description:
 * @author: zhangjunfa
 * @date: 2023/5/6 17:59
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getUserByUsername(username);
        //如果查询不到数据就通过抛出异常来给出提示
        if (Objects.isNull(sysUser)) {
            throw new RuntimeException("用户名或密码错误");
        }
        // TODO: 2023/5/7 正式项目中都是从数据库中获取数据 ，暂时先做模拟
        Set<String> list = new HashSet<>(Arrays.asList("test"));

        //封装成UserDetails对象返回
        return new LoginUser(sysUser, list);
    }
}
